﻿// =====================================================
// Copyright (C) 2016-2021 Fanjia
// All rights reserved
// 机器名称: PC-LIWEI
// 创建时间: 2021/4/30 20:55:38
// 创 建 人: 李伟
// ======================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FanjiaHotelPlatform.utils.Kits;
using FanJiaCommon.Kits;
using Maticsoft.DBUtility;
using Newtonsoft.Json;

namespace WindowsFormsApplication1
{
    public class HotelData
    {
        public void ImportJsonData()
        {
            try
            {
                StreamReader sr = new StreamReader("D:\\HotelInfo\\Hotels_2021-04-28.json");
                String jsonStr = sr.ReadToEnd();

                var hotelInfos = JsonKit.DeserializeJsonToList<HotelInfo>(jsonStr);

                var saveList  = new List<SaveHotelInfo>();

                var dtSave = DbHelperSQL.QueryM("SELECT * FROM [HotelInfo] WHERE 1=2", 2).Tables[0];

                hotelInfos?.ForEach(h =>
                {
                    DataRow dr = dtSave.NewRow();

                    dr["HotelCode"] = h.HotelCode;
                    dr["CityCode"] = h.CityCode;
                    dr["Name"] = h.Name;
                    dr["Address"] = h.Address;
                    dr["MainPicture"] = h.MainPicture;
                    dr["Longitude"] = h.Longitude;
                    dr["Latitude"] = h.Latitude;
                    dr["StarRating"] = h.StarRating;
                    dr["OpeningTime"] = h.OpeningTime;
                    dr["FixTime"] = h.FixTime;
                    dr["PhoneNum"] = h.PhoneNum;
                    dr["FaxNum"] = h.FaxNum;
                    dr["Introduction"] = h.Introduction;
                    dr["Facilities"] = h.Facilities;
                    dr["RoomNumber"] = h.RoomNumber;
                    dr["Status"] = h.Status;
                    dr["UpdateTimeStamp"] = h.UpdateTimeStamp;
                    dr["HotelImgs"] = h.HotelImgs?.Count > 0 ? string.Join(",", h.HotelImgs.ToArray()) : "";
                    dr["HotelFacilities"] = h.HotelFacilities?.Count > 0 ? string.Join(",", h.HotelFacilities.ToArray()) : "";
                    dr["SyncMode"] = h.SyncMode;

                    dtSave.Rows.Add(dr);

                    //saveList.Add(new SaveHotelInfo
                    //{
                    //    Address = h.Address,
                    //    CityCode = h.CityCode,
                    //    Facilities = h.Facilities,
                    //    FaxNum = h.FaxNum,
                    //    FixTime = h.FixTime,
                    //    HotelCode = h.HotelCode,
                    //    HotelFacilities =
                    //        h.HotelFacilities?.Count > 0 ? string.Join(",", h.HotelFacilities.ToArray()) : "",
                    //    HotelImgs = h.HotelImgs?.Count > 0 ? string.Join(",", h.HotelImgs.ToArray()) : "",
                    //    Introduction = h.Introduction,
                    //    Latitude = h.Latitude,
                    //    Longitude = h.Longitude,
                    //    Name = h.Name,
                    //    StarRating = h.StarRating,
                    //    OpeningTime = h.OpeningTime,
                    //    RoomNumber = h.RoomNumber,
                    //    Status = h.Status,
                    //    UpdateTimeStamp = h.UpdateTimeStamp,
                    //    MainPicture = h.MainPicture,
                    //    SyncMode = h.SyncMode,
                    //    PhoneNum = h.PhoneNum
                    //});
                });

                DbHelperSQL.SaveListDataBatM(dtSave, "HotelInfo", 2);

                MessageBox.Show(hotelInfos.Count.ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            
        }

        public void ExportToJson()
        {
            var dtSave = DbHelperSQL.QueryM("SELECT * FROM [HotelInfo_N]").Tables[0];
            var jsonList = new List<HotelInfo>();

            if (dtSave.Rows.Count > 0)
            {
                foreach (DataRow dr in dtSave.Rows)
                {
                    jsonList.Add(new HotelInfo
                    {
                        HotelCode = Convert.ToString(dr["HotelCode"]),
                        CityCode = Convert.ToString(dr["CityCode"]),
                        Name = Convert.ToString(dr["Name"]),
                        Address = Convert.ToString(dr["Address"]),
                        MainPicture = Convert.ToString(dr["MainPicture"]),
                        Longitude = Convert.ToString(dr["Longitude"]),
                        Latitude = Convert.ToString(dr["Latitude"]),
                        StarRating = Convert.ToString(dr["StarRating"]),
                        OpeningTime = Convert.ToString(dr["OpeningTime"]),
                        FixTime = Convert.ToString(dr["FixTime"]),
                        PhoneNum = Convert.ToString(dr["PhoneNum"]),
                        FaxNum = Convert.ToString(dr["FaxNum"]),
                        Introduction = Convert.ToString(dr["Introduction"]),
                        Facilities = Convert.ToString(dr["Facilities"]),
                        RoomNumber = ConvertKit.ToInt32(dr["RoomNumber"]),
                        Status = ConvertKit.ToInt32(dr["Status"]),
                        UpdateTimeStamp = ConvertKit.ToInt32(dr["UpdateTimeStamp"]),
                        HotelImgs = GetStrList(dr["HotelImgs"]),
                        HotelFacilities = GetStrList(dr["HotelFacilities"]),
                        SyncMode = ConvertKit.ToInt32(dr["SyncMode"]),
                        QtHotelCode = Convert.ToString(dr["QtHotelCode"])
                    });

                }

                Export(jsonList, "D:\\HotelInfo\\New.json");

                MessageBox.Show("Done!");
            }
        }

        private List<string> GetStrList(object o)
        {
            try
            {
                var rs = o.ToString().Split(',');
                return rs.ToList();
            }
            catch (Exception e)
            {
                return null;
            }
        }



        /// <summary>
        /// 导出文件
        /// </summary>
        public void Export(List<HotelInfo> dicts, string filepath)
        {
            //File.WriteAllText(filepath, JsonConvert.SerializeObject(dicts, Formatting.Indented));
            File.WriteAllText(filepath, JsonConvert.SerializeObject(dicts));
        }
    }

    public class HotelInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public string HotelCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string CityCode { get; set; }
        /// <summary>
        /// 维也纳酒店（安徽黄山歙县徽州古城店）
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 安徽省黄山市歙县黄山东路50号（天都大厦）
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string MainPicture { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Longitude { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Latitude { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string StarRating { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string OpeningTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FixTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string PhoneNum { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FaxNum { get; set; }
        /// <summary>
        /// 维也纳酒店（安徽黄山歙县徽州古城店）是一座按四星级标准兴建的精品商务酒店，酒店总建筑面积约4000平方米，集高档客房、美食、会议为一体，拥有95间高贵、典雅、高性价比的豪华客房，网络、WIFI、独立卫生间等一应俱全，是商旅人士的首选。酒店采用欧陆风情设计，以欧洲新古典主义为主体建筑风格，配以欧洲艺术佳作装饰，为宾客营造出魅力无穷的视觉享受，完美诠释新古典主义商务酒店新概念。酒店地处黄山市歙县黄山东路50号天都大厦（歙县人民医院斜对面），距离徽州古城仅1.5公里，高铁站5.6公里、火车站3公里。毗邻国家5A旅游圣地中国黄山风景区，太平湖，宏村，交通便利，闹中取静周边餐饮、娱乐、超市配套齐全。
        /// </summary>
        public string Introduction { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Facilities { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int RoomNumber { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int UpdateTimeStamp { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> HotelImgs { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> HotelFacilities { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int SyncMode { get; set; }

        //public int IsQtMapping { get; set; }

        public string QtHotelCode { get; set; }
    }

    public class SaveHotelInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public string HotelCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string CityCode { get; set; }
        /// <summary>
        /// 维也纳酒店（安徽黄山歙县徽州古城店）
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 安徽省黄山市歙县黄山东路50号（天都大厦）
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string MainPicture { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Longitude { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Latitude { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string StarRating { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string OpeningTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FixTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string PhoneNum { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FaxNum { get; set; }
        /// <summary>
        /// 维也纳酒店（安徽黄山歙县徽州古城店）是一座按四星级标准兴建的精品商务酒店，酒店总建筑面积约4000平方米，集高档客房、美食、会议为一体，拥有95间高贵、典雅、高性价比的豪华客房，网络、WIFI、独立卫生间等一应俱全，是商旅人士的首选。酒店采用欧陆风情设计，以欧洲新古典主义为主体建筑风格，配以欧洲艺术佳作装饰，为宾客营造出魅力无穷的视觉享受，完美诠释新古典主义商务酒店新概念。酒店地处黄山市歙县黄山东路50号天都大厦（歙县人民医院斜对面），距离徽州古城仅1.5公里，高铁站5.6公里、火车站3公里。毗邻国家5A旅游圣地中国黄山风景区，太平湖，宏村，交通便利，闹中取静周边餐饮、娱乐、超市配套齐全。
        /// </summary>
        public string Introduction { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Facilities { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int RoomNumber { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int UpdateTimeStamp { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string HotelImgs { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string HotelFacilities { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int SyncMode { get; set; }

  
    }

}
